//
//  HomeDataModel.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/27.
//

import UIKit

struct HomeDataModel: Codable {
    var considerization: Considerization?
    var pachist: [PachistItem]?
    var memberite: [Memberite]?
}

// 客服配置模型
struct Considerization: Codable {
    var agencyature: String?
    var disitor: String?
}

struct PachistItem: Codable {
    let olig: String
    let hemerhood: HemerhoodValue
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.olig = try container.decode(String.self, forKey: .olig)
        
        // 根据 olig 值直接使用具体类型，避免多态解析
        switch olig {
        case "worker": // BANNER
            let items = try container.decode([BannerItem].self, forKey: .hemerhood)
            self.hemerhood = .banner(items)
        case "cand": // REPAY
            let items = try container.decode([RepayItem].self, forKey: .hemerhood)
            self.hemerhood = .repay(items)
        case "cristhealthan": // PRODUCT_LIST
            let items = try container.decode([ProductListItem].self, forKey: .hemerhood)
            self.hemerhood = .productList(items)
        case "quatiturnade", "quiteoon": // LARGE_CARD, SMALL_CARD
            // 对于卡片类型，先尝试解码为单个对象，再尝试数组
            do {
                let item = try container.decode(ProductCard.self, forKey: .hemerhood)
                self.hemerhood = .productCard([item])
            } catch {
                // 如果单个对象解码失败，尝试解码为数组
                let items = try container.decode([ProductCard].self, forKey: .hemerhood)
                self.hemerhood = .productCard(items)
            }
        default:
            // 对于未知类型，先尝试数组，再尝试对象
            do {
                let items = try container.decode([BannerItem].self, forKey: .hemerhood)
                self.hemerhood = .banner(items)
            } catch {
                let item = try container.decode(ProductCard.self, forKey: .hemerhood)
                self.hemerhood = .productCard([item])
            }
        }
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(olig, forKey: .olig)
        
        switch hemerhood {
        case .banner(let items):
            try container.encode(items, forKey: .hemerhood)
        case .repay(let items):
            try container.encode(items, forKey: .hemerhood)
        case .productList(let items):
            try container.encode(items, forKey: .hemerhood)
        case .productCard(let items):
            // 如果只有一个元素，编码为单个对象；否则编码为数组
            if items.count == 1 {
                try container.encode(items[0], forKey: .hemerhood)
            } else {
                try container.encode(items, forKey: .hemerhood)
            }
        }
    }
    
    enum CodingKeys: String, CodingKey {
        case olig, hemerhood
    }
}

// 简化的 HemerhoodValue，使用具体类型
enum HemerhoodValue {
    case banner([BannerItem])
    case repay([RepayItem])
    case productList([ProductListItem])
    case productCard([ProductCard])
}

// 计算属性也相应简化
extension PachistItem {
    var bannerItems: [BannerItem] {
        if case .banner(let items) = hemerhood {
            return items
        }
        return []
    }
    
    var repayItems: [RepayItem] {
        if case .repay(let items) = hemerhood {
            return items
        }
        return []
    }
    
    var productListItems: [ProductListItem] {
        if case .productList(let items) = hemerhood {
            return items
        }
        return []
    }
    
    var productCards: [ProductCard] {
        if case .productCard(let items) = hemerhood {
            return items
        }
        return []
    }
    
    // 兼容旧代码，返回第一个产品卡片
    var productCard: ProductCard? {
        return productCards.first
    }
}

// Banner 广告项
struct BannerItem: Codable {
    var trigesim: String?
    var labiwise: String?
}

// 产品卡片模型（用于 LARGE_CARD 和 SMALL_CARD）
struct ProductCard: Codable {
    var selenify: Int?
    var fascletterism: String?
    var cutad: String?
    var politward: String?
    var majorization: String?
    var six: String?
    var glass: String?
    var pteraid: String?
    var thisfication: String?
    var focusel: String?
    var voluntful: String?
    var costatory: String?
}

// 还款提醒项
struct RepayItem: Codable {
    var pitister: String?
    var malion: String?
    var clositious: String?
    var trigesim: String?
}

// 产品列表项
struct ProductListItem: Codable {
    var selenify: Int?
    var fascletterism: String?
    var majorization: String?
    var six: String?
    var glass: String?
    var pollicworldie: String?
    var focusel: String?
    var politward: String?
    var introalongular: [String]?
    var rheably: String?
    var cutad: String?
    var brom: String?
    var radioage: String?
    var sento: Int?
    var culpive: String?
    var tidiory: String?
    var nothingform: Int?
    var sebiminuteful: String?
    var trigesim: String?
    var itselfarian: Int?
    var midi: [String]?
    var centrupot: String?
    var amph: [String]?
    var staff: Int?
    var agogice: String?
    var thisfication: String?
}

// 用户额度项
struct Memberite: Codable {
    var equhigh: String?
    var clositious: String?
    var nict: String?
}

